package com.baseframe.topic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.tool.result.R;
import com.baseframe.topic.entity.SnsTestTopic;
import com.baseframe.topic.entity.SnsTestTopicUser;
import com.baseframe.topic.entity.request.SnsTestTopicRequest;
import com.baseframe.topic.entity.request.SnsTestTopicUserRequest;
import com.baseframe.topic.entity.vo.SnsTestTopicVO;
import com.baseframe.topic.service.SnsTestTopicService;
import com.baseframe.topic.service.SnsTestTopicUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 前台控制器类，处理SNS测试话题相关的请求。
 */
@RestController
@RequestMapping("/front/test-topic")
@AllArgsConstructor
public class FrontSnsTestTopicController {

    private SnsTestTopicService snsTestTopicService;
    private SnsTestTopicUserService snsTestTopicUserService;

    /**
     * 获取能测分页信息
     *
     * @param pageInfo 分页信息对象
     * @param request  SNS测试话题请求对象
     * @return R对象，包含能测分页查询结果信息
     */
    @GetMapping("/page")
    public R<Object> getTestTopicPageList(PageInfo pageInfo, SnsTestTopicRequest request) {
        // 调用服务层进行能测分页查询操作，并返回查询结果
        return R.data(snsTestTopicService.getTestTopicPageList(pageInfo, request));
    }

    /**
     * 获取能测详情
     *
     * @param request SNS测试话题请求对象
     * @return R对象，包含能测详情信息
     */
    @GetMapping("/details")
    public R<Object> getTestTopicDetails(SnsTestTopicRequest request) {
        // 检查必要的请求参数是否存在
        if (request.getId() == null) {
            return R.fail("缺少必要请求参数");
        }

        // 检查用户是否已经参与过该能测
        boolean join = snsTestTopicUserService.hasJoin(request.getId());
        if (join) {
            return R.fail("你已经参与过该能测了，无法继续参与~");
        }

        // 调用服务层获取能测详情，并返回查询结果
        SnsTestTopicVO snsTestTopicVO = snsTestTopicService.getTestTopicDetails(request);
        return snsTestTopicVO != null ? R.data(snsTestTopicVO) : R.fail("能测不存在~");
    }

    /**
     * 提交能测答案
     *
     * @param request SNS测试话题请求对象
     * @return R对象，包含答案提交结果信息
     */
    @PostMapping("/submit")
    public R<Object> submit(@RequestBody SnsTestTopicRequest request) {
        // 检查必要的请求参数是否存在
        if (request.getId() == null) {
            return R.fail("答案提交失败~");
        }

        // 检查用户是否已经提交过该能测
        SnsTestTopicUser one = snsTestTopicUserService.getOne(new LambdaQueryWrapper<SnsTestTopicUser>()
                .eq(SnsTestTopicUser::getUserId, AuthUtil.getUserId())
                .eq(SnsTestTopicUser::getTopicId, request.getId())
                .last("limit 1")
        );
        if (one != null) {
            return R.fail("你已经提交了本能测,无需重复提交~");
        }

        // 设置用户ID和话题ID，并调用服务层提交答案
        if (request.getUserSelectOptions() != null) {
            for (SnsTestTopicUserRequest userSelectOption : request.getUserSelectOptions()) {
                userSelectOption.setTopicId(request.getId());
                userSelectOption.setUserId(AuthUtil.getUserId());
            }
        }

        // 调用服务层进行用户答案的提交，并返回提交结果
        return snsTestTopicUserService.submitUserSelectOptions(request) ? R.success() : R.fail();
    }
}

